#include <pthread.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <unistd.h>
using namespace std;

#define DataType int
#define TDataBlock ThreadData<BlockQueue<DataType>>
#define TYPE Thread<ThreadData<BlockQueue<DataType>>>

class LockGuard
{
public:
    LockGuard(pthread_mutex_t* mutex)
        :_mutex(mutex)
    {
        pthread_mutex_lock(_mutex);
    }

    ~LockGuard()
    {
        pthread_mutex_unlock(_mutex);
    }

private:
    pthread_mutex_t* _mutex;

};

//  T --->  BlockQueue<DataType>
template<class T>
class ThreadData
{
public:
    ThreadData(T* data, string& name)
        :_data(data)
        ,_name(name)
    {}

    T* _data = nullptr;              //临界资源
    string _name = " ";
};

//  T --->  ThreadData<BlockQueue<DataType>>
template<class T>
class Thread
{
public:
    typedef function<void(T*)> func_t;

    Thread(func_t func, T* data)
        :_func(func)
        ,_data(data)
    {}

    static void* routine(void* args)
    {
        Thread* thread = reinterpret_cast<Thread*>(args);
        thread->_func(thread->_data);

        return nullptr;
    }

    void CreateRun()
    {
        int n = pthread_create(&_tid, nullptr, routine, this);
        if(n != 0) cout << "create: " << strerror(n) << endl;
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
    }

    void detach()
    {
        pthread_detach(_tid);
    }

private:
    //希望能够得到临界资源
    T* _data;

    pthread_t _tid;
    func_t _func;

};



